import $C from './config.js';
import $U from './util.js';
// import $store from '@/store/index.js';
export default {
	// 全局配置
	common: {
		baseUrl: $C.baseUrl,
		header: {
			'Content-Type': 'application/json;charset=UTF-8',
		},
		data: {},
		method: 'GET',
		dataType: 'json',
		authCode: true
	},
	// 请求 返回promise
	request(options = {}) {
		// 组织参数
		options.url = this.common.baseUrl + options.url
		options.header = options.header || this.common.header
		options.data = options.data || this.common.data
		options.method = options.method || this.common.method
		options.dataType = options.dataType || this.common.dataType
		options.authCode = options.authCode === false ? false : this.common.authCode

		// 请求之前验证...
		
		// authCode验证
		if (options.authCode) {
			let authCode = uni.getStorageSync('authCode')
			console.log("authCode")
			console.log(authCode)
			// 二次验证
			// if (!authCode) {
			//     uni.showToast({ title: '请先登录', icon: 'none' });
			//     // authCode不存在时跳转
			//     return uni.reLaunch({
			//         url: '/pages/login/login',
			//     });
			// }
			// 往header头中添加authCode
			options.header.authCode = authCode
		}

		// 请求
		return new Promise((res, rej) => {
			// 请求中...

			uni.request({
				...options,
				sslVerify: false,
				success: (result) => {
					// console.log(result);
					// 返回原始数据
					if (options.native) {
						return res(result)
					}
					// 服务端失败
					if (result.statusCode !== 200) {
						if (options.toast !== false) {
							uni.showToast({
								title: result.data.message || '服务端失败',
								icon: 'none'
							});
						}
						// authCode不合法，直接退出登录
						if (result.data.message === 'authCode 令牌不合法!') {
							// $store.dispatch('logout')
						}
						return rej(result.data)
					}
					// 其他验证...
					// if (!uni.getStorageSync('userId') || uni.getStorageSync('userId') == '-') {
					// 	return uni.reLaunch({
					// 		url: '/pages/login/login',
					// 	});
					// }
					// 成功
					let data = result.data
					if (data.status === 20005) {
						uni.showToast({
							title: data.message,
							icon: 'none'
						});
						//     // authCode不存在时跳转
						return uni.reLaunch({
							url: '/pages/login/login',
						});
						// } else if (data.status !== 0) {
						// 	uni.$showMsg(data.message)
					} else {
						res(data)
						if (options.authCode) {
							if (!uni.getStorageSync('userId') || uni.getStorageSync('userId') == '-') {
								return uni.reLaunch({
									url: '/pages/login/login',
								});
							}
						}
					}

				},
				fail: (error) => {
					uni.showToast({
						title: error.errMsg || '请求失败',
						icon: 'none'
					});
					return rej(error)
				}
			});
		})
	},
	// get请求
	get(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.request(options)
	},
	// post请求
	post(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	},
	// delete请求
	del(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'DELETE'
		return this.request(options)
	},
	// 上传文件
	upload(url, data, onProgress = false) {
		return new Promise((result, reject) => {
			// 上传
			let authCode = $U.getStorage('authCode')
			if (!authCode) {
				uni.showToast({
					title: '请先登录',
					icon: 'none'
				});
				// authCode不存在时跳转
				return uni.reLaunch({
					url: '/pages/login/login',
				});
			}

			const uploadTask = uni.uploadFile({
				url: this.common.baseUrl + url,
				filePath: data.filePath,
				name: data.name || "files",
				header: {
					authCode
				},
				success: (res) => {
					if (res.statusCode !== 200) {
						result(false)
						return uni.showToast({
							title: '上传失败',
							icon: 'none'
						});
					}
					let message = JSON.parse(res.data)
					result(message.data);
				},
				fail: (err) => {
					console.log(err);
					reject(err)
				}
			})

			uploadTask.onProgressUpdate((res) => {
				if (typeof onProgress === 'function') {
					onProgress(res.progress)
				}
			});

		})
	}
}
